class Solution:
    def isPathCrossing(self, path: str) -> bool:
        pos = (0, 0)
        visited = {pos}
        for c in path:
            x, y = pos
            if c == 'N':
                y += 1
            elif c == 'S':
                y -= 1
            elif c == 'E':
                x += 1
            elif c == 'W':
                x -= 1
            if (x, y) in visited:
                return True
            else:
                pos = (x, y)
                visited.add(pos)
        return False
